package org.blogsite.greencode.dao.impl;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.blogsite.greencode.bean.TeachingSupport_Resource;
import org.blogsite.greencode.dao.TeachingSupport_ResourceDAO;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * @author：wzj
 * E-mail: jsun789@gmail.com
 * @date：2010-7-31 下午06:26:39
 */
public class TeachingSupport_ResourceDAOImpl extends HibernateDaoSupport implements
		TeachingSupport_ResourceDAO{
/*改方法弃用了.*/
	public void addResource(TeachingSupport_Resource teachingSupportResource) {

		try {
			getHibernateTemplate().save(teachingSupportResource);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	/*设置上传文件的文件名*/
	public String generateFileName(String fileName) {
		
		DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
		String formatDate = format.format(new Date());
		int random = new Random().nextInt(10000);
		int position = fileName.lastIndexOf(".");
		String extension = fileName.substring(position);
		// 返回,日期+时分秒+随机生成
		return formatDate + random + extension;
	}
	/*查询所有资源*/
	public List getAllResource(){
		try {
			
			return getHibernateTemplate().find("from TeachingSupport_Resource tsr");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/*查询指定名称的资源*/
	public List getResourceByName(String name){
		try {
			
			return getHibernateTemplate().find("from TeachingSupport_Resource tsr where tsr.name=?",name);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/*查询指定课程名资源*/
	public List getResourceByCname(String name){
		try {
//			System.out.println("cname");
			return getHibernateTemplate().find("from TeachingSupport_Resource tsr where tsr.courseName=?",name);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/*模糊查询资源*/
	public List getResourceByBlear(String text){
		try {
			
			return getHibernateTemplate().find("from TeachingSupport_Resource tsr  where tsr.name like ? or tsr.courseName like ? or tsr.describe like ?",new String[]{"%"+text+"%","%"+text+"%","%"+text+"%"});
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	//修改资源信息
	public  void updateResource(TeachingSupport_Resource teachingSupportResource){
		try {
//			System.out.println("nameq:"+teachingSupportResource.getName()+"qq:"+teachingSupportResource.getId());
			getHibernateTemplate().update(teachingSupportResource);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//删除资源信息
	public void deleteResource(TeachingSupport_Resource teachingSupportResource){
		try {
			getHibernateTemplate().delete(teachingSupportResource);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
